<template>
    <template v-if="playbackInfo.src">
        <div
            class="music_player"
            v-if="!screenfulViewStatus"
            @click="screenfulViewStatus=true"
        >
            <div class="music_info">
                <img class="music_cover" :src="musicInfo.cover">
                <div class="music_info_main">
                    <p class="music_name">{{musicInfo.name}}</p>
                    <p class="music_author">{{musicInfo.author}}</p>
                </div>
            </div>
            <div class="player_menu">
                <span class="btn">
                    <i
                        v-if="playbackInfo.state === 'play'"
                        @click.stop="pauseFn"
                        class="iconfont icon-suspend"
                    />
                    <i
                        @click.stop="playFn"
                        v-else-if="playbackInfo.state === 'pause'"
                        class="iconfont icon-play"
                    />
                    <van-loading
                        v-else-if="playbackInfo.state === 'loading'"
                        color="#fff"
                        size="24px"
                    />
                    <i
                        v-else-if="playbackInfo.state === 'ended'"
                        @click.stop="replayFn"
                        class="iconfont icon-stop"
                    />
                </span>
                <span class="btn" @click.stop="nextFn">
                    <i class="iconfont icon-next"></i>
                </span>
                <span class="btn" @click.stop="downloadFn">
                    <i class="iconfont icon-download"></i>
                </span>
            </div>
        </div>
        <van-popup
            class="music_player_panel"
            v-model:show="screenfulViewStatus"
            closeable
            close-icon-position="top-left"
            :lazy-render="false"
            position="bottom"
            :style="{ height: '100%', background: '#333'}"
            @close="screenful_view_status=false"
        >
            <div class="music_info">
                <img class="music_cover" :src="musicInfo.cover">
                <div class="music_info_main">
                    <p class="music_name">{{musicInfo.name}}</p>
                    <p class="music_author">{{musicInfo.author}}</p>
                </div>
            </div>
            <div class="progress_bar">
                <van-slider
                    active-color="#2ca2f9"
                    :min="0"
                    :max="playbackInfo.total_time"
                    v-model="playbackInfo.current_time"
                    @drag-end="changePlaybackTime"
                    @change="changePlaybackTime"
                >
                    <template #button>
                        <div class="slider-button"></div>
                    </template>
                </van-slider>
            </div>
            <div class="player_menu">
                <span class="btn" @click="preFn">
                    <i class="iconfont icon-prev" />
                </span>
                <span class="btn">
                    <i
                        v-if="playbackInfo.state === 'play'"
                        @click.stop="pauseFn"
                        class="iconfont icon-suspend"
                    />
                    <i
                        @click.stop="playFn"
                        v-else-if="playbackInfo.state === 'pause'"
                        class="iconfont icon-play"
                    />
                    <van-loading
                        v-else-if="playbackInfo.state === 'loading'"
                        color="#fff"
                        size="24px"
                    />
                    <i
                        v-else-if="playbackInfo.state === 'ended'"
                        @click.stop="replayFn"
                        class="iconfont icon-stop"
                    />
                </span>
                <span class="btn" @click="nextFn">
                    <i class="iconfont icon-next"></i>
                </span>
            </div>
            <div class="download" @click.stop="downloadFn">
                <i class="iconfont icon-download"></i>
                下载这首歌
            </div>
        </van-popup>
    </template>
</template>

<script>
import { Slider, Popup, Loading } from 'vant'
import { ref, reactive } from 'vue'
export default {
    name: 'MusicPlayer',
    components: {
        VanSlider: Slider,
        VanPopup: Popup,
        VanLoading: Loading
    },
    setup () {
        const screenfulViewStatus = ref(false)
        const musicInfo = reactive({
            cover: '',
            name: '',
            author: ''
        })
        const audio = ref(null)
        const playbackInfo = reactive({
            state: false, // 播放状态，pause || play || loading || ended
            timer: null,
            current_time: 0,
            total_time: Infinity
        })

        const playFn = () => {
            // 播放
            playbackInfo.state = 'play'
            audio.value.play()
        }

        const pauseFn = () => {
            // 暂停
            playbackInfo.state = 'pause'
            audio.value.pause()
            if (playbackInfo.timer !== null) {
                clearInterval(playbackInfo.timer)
            }
        }

        const replayFn = () => {
            // 重新播放
            audio.value.currentTime = 0
            playFn()
        }

        const initFn = async (cover, songname, singername, src) => {
            // 初始化播放信息
            playbackInfo.state = 'loading'
            musicInfo.cover = cover
            musicInfo.name = songname
            musicInfo.author = singername
            playbackInfo.src = src
            // 更换播放源
            if (audio.value !== null) {
                audio.value.pause()
                audio.value = null
                if (playbackInfo.timer !== null) {
                    clearInterval(playbackInfo.timer)
                }
            }
            audio.value = new Audio(src)
            // 监听是否可以播放
            audio.value.addEventListener('canplay', () => {
                // 播放音乐，并获取音频信息
                playFn()
                playbackInfo.current_time = audio.value.currentTime
                playbackInfo.total_time = audio.value.duration
                // 开启播放时长监听
                playbackInfo.timer = setInterval(() => {
                    playbackInfo.current_time = audio.value.currentTime
                }, 1000)
            })
            // 检测是否播放结束
            audio.value.addEventListener('ended', () => {
                playbackInfo.state = 'ended'
            })
        }

        const changePlaybackTime = () => {
            // 更改当前播放时间
            audio.value.currentTime = playbackInfo.current_time
        }

        return {
            screenfulViewStatus,
            musicInfo,
            audio,
            playbackInfo,
            initFn,
            changePlaybackTime,
            playFn,
            pauseFn,
            replayFn
        }
    }
}
</script>

<style lang="scss" scoped>
.music_player {
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 1000;
    display: flex;
    justify-content: space-between;
    width: 100%;
    background: #333;
    .music_info {
        display: flex;
        padding: 10px 15px;
        .music_cover {
            width: 50px;
            height: 50px;
        }
        .music_info_main {
            display: flex;
            flex-flow: column;
            justify-content: center;
            margin-left: 10px;
            color: #fff;
            .music_name {
                width: 100%;
                line-height: 1.7;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
                font-size: .8571rem;
                color: #fff;
            }
            .music_author {
                line-height: 1.3;
                font-size: .7143rem;
                color: #888;
            }
        }
    }
    .player_menu {
        display: flex;
        align-items: center;
        margin-right: 15px;
        .btn {
            margin-left: 15px;
            i {
                font-size: 24px;
                color: #fff;
            }
        }
    }
}
.music_player_panel {
    display: flex;
    z-index: 1000 !important;
    .music_info {
        display: flex;
        flex-flow: column;
        align-content: center;
        padding: 0 30px;
        margin-top: 30px;
        .music_info_main {
            display: flex;
            flex-flow: column;
            justify-content: center;
            align-items: center;
            margin-left: 10px;
            margin-bottom: 15px;
            color: #fff;
            .music_name {
                line-height: 1.7;
                white-space: nowrap;
                text-overflow: ellipsis;
                font-size: 1.1rem;
                color: #fff;
            }
            .music_author {
                line-height: 1.3;
                font-size: .7143rem;
                color: #888;
            }
        }
        .music_cover {
            width: 10.7143rem;
            height: 10.7143rem;
            margin: 35px auto;
        }
    }
    .progress_bar {
        margin: 10px 20px;
    }
    .slider-button {
        width: 8px;
        height: 8px;
        border-radius: 4px;
        background: #2ca2f9;
    }
    .player_menu {
        display: flex;
        justify-content: center;
        .btn {
            margin: 10px;
            padding: 10px;
            border-radius: 50%;
            border: 1px solid #fff;
            i {
                color: #fff;
                font-size: 1.3rem;
                font-weight: bold;
            }
        }
    }
    .download {
        width: 160px;
        margin-top: 20px;
        margin-left: auto;
        margin-right: auto;
        padding: 10px;
        text-align: center;
        background: #2ca2f9;
        color: #fff;
    }
}
</style>
